package com.mutual.productPay.service.impl;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.google.common.collect.Maps;
import com.mutual.appUserInfo.pojo.AppUserInfo;
import com.mutual.appUserInfo.pojo.AppUserInfoProduct;
import com.mutual.appUserInfo.service.AppUserInfoProductService;
import com.mutual.appUserInfo.service.AppUserInfoService;
import com.mutual.commom.pojo.ReturnResult;
import com.mutual.commom.util.SystemUtil;
import com.mutual.productPay.dao.ProductPayDao;
import com.mutual.productPay.pojo.ProductPay;
import com.mutual.productPay.service.ProductPayService;

@Service
public class ProductPayServiceImpl implements ProductPayService {

	@Resource
	private ProductPayDao productPayDao;
	
	@Resource
	private AppUserInfoProductService appUserInfoProductService;
	
	@Resource
	private AppUserInfoService appUserInfoService;
	
	@Override
	public void insert(ProductPay entity) {
		productPayDao.insertSelective(entity);
	}

	@Override
	public void updateByPrimaryKey(ProductPay entity) {
		productPayDao.updateByPrimaryKeySelective(entity);
	}

	@Override
	public ProductPay selectByPrimaryKey(Serializable entityid) {
		return productPayDao.selectByPrimaryKey(entityid);
	}

	@Override
	public List<Map<String, Object>> selectStatisticsProdductPayList(Map<String, Object> param) {
		if(param.containsKey("page")){
			int pageNo=SystemUtil.getPageNo(param.get("page"));
			int pageSize=Integer.parseInt(param.get("limit").toString());
			param.put("startNo", (pageNo-1)*pageSize);
			param.put("pageSize", pageSize);
		}
		return productPayDao.selectStatisticsProdductPayList(param);
	}
	
	@Transactional
	@Override
	public ReturnResult deduction(ProductPay productPay) {
		ReturnResult result=new ReturnResult();
		Map<String,Object> param=Maps.newHashMap();
		param.put("productId", productPay.getProductId());
		param.put("notIncludedIdCode", productPay.getPaymentIdCode());
		int checkCount=productPayDao.selectTotalCount(param);
		if(checkCount>0) {
			result.setStatus(ReturnResult.STATUS_FAILED);
			result.setReturnMsg("扣款失败，该身份证号已经扣过款了");
			return result;
		}
		List<AppUserInfoProduct> appUserInfoProducts=appUserInfoProductService.selectAppUserInfoProductList(param);
		if(appUserInfoProducts.isEmpty()){
			result.setStatus(ReturnResult.STATUS_FAILED);
			result.setReturnMsg("电子不存在");
			return result;
		}
		boolean flag = true;
		StringBuilder su=new StringBuilder();
		su.append("扣款失败，一下电子凭证信息金额不够，无法扣款<br><br>");
		for(AppUserInfoProduct v:appUserInfoProducts){
			//平均扣款金额大于所剩金额
			if(v.getAmount()==null||v.getAmount().compareTo(BigDecimal.ZERO)==0
					||v.getAmount().subtract(productPay.getAverageAmount()).compareTo(BigDecimal.ZERO)<0){
				flag=false;
				su.append("电子凭证身份证号：").append(v.getIdCode()).append("，凭证姓名：")
				  .append(v.getApplyPeopleName()).append("<br>");
				su.append("凭证类型：").append(v.getType()==AppUserInfoProduct.TYPE_FAMILY?"家人，":"个人，");
				AppUserInfo userInfo = appUserInfoService.selectByPrimaryKey(v.getAppUserId());
				if(v.getType()==AppUserInfoProduct.TYPE_FAMILY){
					su.append("联系人：").append(userInfo.getRealName()).append(",");
				}
				su.append("手机号：").append(userInfo.getMobile()).append("<br><br>");
				continue;
			}
		}
		if(!flag){
			result.setStatus(ReturnResult.STATUS_FAILED);
			result.setReturnMsg(su.toString());
			return result;
		}
		AppUserInfoProduct appUserInfoProduct=null;
		for(AppUserInfoProduct v:appUserInfoProducts){
			appUserInfoProduct=new AppUserInfoProduct();
			appUserInfoProduct.setId(v.getId());
			appUserInfoProduct.setDeductionCount((v.getDeductionCount()==null?0:v.getDeductionCount())+1);
			appUserInfoProduct.setAmount(v.getAmount().subtract(productPay.getAverageAmount()));
			appUserInfoProductService.updateByPrimaryKey(appUserInfoProduct);
		}
		int totalAppUserInfoProduct=appUserInfoProducts.size();
		productPay.setDeductionNumberPeople(totalAppUserInfoProduct);
		BigDecimal paBigDecimal=productPay.getAverageAmount().multiply(BigDecimal.valueOf(totalAppUserInfoProduct));
		productPay.setPaymentTotalAmount(paBigDecimal);
		productPayDao.insertSelective(productPay);
		return result;
	}
	
	public static void main(String[] args) {
		BigDecimal b=new BigDecimal("3");
		System.out.println(b.subtract(BigDecimal.valueOf(4)).compareTo(BigDecimal.ZERO));
	}

	@Override
	public int selectProductPayCount(Map<String, Object> param) {
		return productPayDao.selectTotalCount(param);
	}

	@Override
	public List<ProductPay> selectProductPayList(Map<String, Object> param) {
		if(param.containsKey("page")){
			int pageNo=SystemUtil.getPageNo(param.get("page"));
			int pageSize=Integer.parseInt(param.get("limit").toString());
			param.put("startNo", (pageNo-1)*pageSize);
			param.put("pageSize", pageSize);
		}
		return productPayDao.selectAllObjPage(param);
	}
}
